﻿using System;
using System.Diagnostics;
using KinonekoSoftware.ArkSpark.Core;
using KinonekoSoftware.CodariaDB.Languages;
using KinonekoSoftware.CodariaDB.Runtimes.Abstractions;
using KinonekoSoftware.CodariaDB.Utils;
using KinonekoSoftware.Foundation.Styles;
using KinonekoSoftware.Platform.Core;
using KinonekoSoftware.Platform.Languages;
using KinonekoSoftware.JuXiaoYouDB.Core;
using KinonekoSoftware.Studio.Internals;
using NLog;

namespace KinonekoSoftware.Studio
{
    public static partial class Platform
    {
        public const string FileName_Setting_UI        = "juxiaoyou4-ui.json";
        public const string FileName_Setting_App       = "juxiaoyou4-app.json";
        public const string FileName_Setting_Repo      = "juxiaoyou4-repo.json";
        public const string FileName_Setting_Watermark = "juxiaoyou4-watermark.json";
        

        public static ApplicationInformation CreateApplicationInformation()
        {
            return new ApplicationInformation
            {
                ApplicationFolder = AppDomain.CurrentDomain.BaseDirectory,
                GlobalFolder      = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "JuXiaoYou"),
                EnableLogger      = true,
            };
        }

        public static ForestSetting GetOrCreateSetting(ApplicationInformation ai)
        {
            var path = Path.Combine(ai.SettingFolder, FileName_Setting_UI);
            var factory = () => new ForestSetting
            {
                EnableExceptionHandler = true,
                EnableLogger           = true,
                Language               = LanguageArea.Chinese,
                Theme                  = Theme.Dark,
            };
            
            var setting = JSON.File<ForestSetting>(path, factory);

            Ioc.Use<ForestSetting>(setting);

            return setting;
        }

        public static Task SaveSetting(ForestSetting setting)
        {
            return Task.Run(async () =>
            {
                var ai       = Ioc.Get<ApplicationInformation>();
                var fileName = Path.Combine(ai.SettingFolder, FileName_Setting_UI);
                await JSON.DumpAsync(setting, fileName);
            });
        }
        
        public static Task SaveSetting(JuXiaoYouSetting setting)
        {
            return Task.Run(async () =>
            {
                var ai       = Ioc.Get<ApplicationInformation>();
                var fileName = Path.Combine(ai.SettingFolder, FileName_Setting_App);
                await JSON.DumpAsync(setting, fileName);
            });
        }

        public static void Init()
        {
            var stopwatch = new Stopwatch();
            var logger    = Ioc.Get<ILogger>();
            stopwatch.Start();
            
            //
            // 添加语言

            LanguageManager.AddProvider(new CodariaLanguageProvider());
            LanguageManager.AddProvider(new ShellLanguageProvider());

            
            #if WINDOWS
            
            LanguageManager.AddProvider(FileLanguageProvider.CreateFromThisDomain());

            #else
            
            LanguageManager.AddProvider(new StudioLanguageProvider());
            
            #endif
            
            stopwatch.Stop();
            logger.Info($"加载语言文本耗时:{stopwatch.ElapsedMilliseconds}ms");

            //
            //
            Ioc.Use<RepositoryRuntime>(RepositoryRuntime.GetDefaultRuntime(logger));
            Ioc.Use<IRepositoryManager, RepositoryManager>(new RepositoryManager());
        }

        public static void Exit()
        {
            //
            // 退出所有Runtime
            Ioc.Get<RepositoryRuntime>().Close();
            
            //
            //
            DisposableCollection.Clear();
        }
        
        public static Task Developing()
        {
            return Ioc.Get<IDialogService>()
                      .Obsoleted(SR.Title_Developing, TextSR.Developing);
        }


        public static bool   IsTrace()          => Ioc.Get<JuXiaoYouSetting>().EnableDebug;
        public static string RepositoryHashCode { get; private set; }
    }
}